$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'StudentInfo/Select',
        datatype: "json",
        colModel: [
            { label: '学号', name: 'studentSchoolId', index: "student_school_id" ,width: 40,key: true },
             { label: '班级名称', name: 'studentClassId',index: "student_class_id" , width: 40 },
             { label: '姓名', name: 'studentName',index: "student_name" , width: 40 },
             { label: '性别', name: 'studentSexual',index: "student_sexual" , width: 40 },
             { label: '出生日期', name: 'studentBirthday', index: "student_birthday" ,width: 40 },
             { label: '民族', name: 'studentEthnicity',index: "student_ethnicity" , width: 40 },
             { label: '学院', name: 'studentDepartment',index: "student_department" , width: 40 },
             { label: '专业名称', name: 'studentProfessionName',index: "student_profession_name" , width: 40 },
             { label: '学制', name: 'studentSchoolingYear',index: "student_schooling_year" , width: 40 },
             { label: '当前所在级', name: 'studentCurrentGrade',index: "student_current_grade" , width: 40 },
             { label: '入学日期', name: 'studentEnrollDate',index: "student_enroll_date" , width: 40 },
             { label: '联系电话', name: 'studentTelNumber',index: "student_tel_number" , width: 40 },
             { label: '专业代码', name: 'studentProfessionCode', index: "student_professionc_ode" ,width: 40 },
             { label: '操作',  name: 'caozuo' ,width: 100, formatter: function(value, options, row){

                 var r= '<a class="little-btn edit-btn" onclick="updateS('+row.studentSchoolId+')"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>'+
                     '&nbsp;&nbsp;<a class="little-btn del-btn" onclick="delS( '+row.studentSchoolId+')"><i class="fa fa-trash-o"></i>&nbsp;删除</a>'

                 return r;
                    // '             <a class="btn btn-primary"><i class="fa fa-trash-o"></i>&nbsp;删除</a>';
            }}
         ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        },
        ondblClickRow:function(rowid){
            var rowData = $("#jqGrid").jqGrid('getRowData',rowid);
            var rowName =$("#jqGrid").jqGrid('getGridParam','colNames');
            var s="";
            var i=2
            for(var key in rowData){
                if(key!='caozuo')
                s=s+rowName[i++]+':'+rowData[key]+'<br> ';

            }
            alert(s);
        }
    });
    $("#exportExcel").attr('href',baseURL+'StudentInfo/export?token='+token);
});
 function updateS(id) {
     $.get(baseURL + "StudentInfo/Info/"+id, function(r){
         vm.showList = false;
         vm.title = "修改";
         vm.StudentInfo = r.StudentInfo;
         vm.studentSchoolId = r.StudentInfo.studentSchoolId;
     });
 }

function delS(id) {
    confirm('确定要删除选中的记录？', function(){
        $.ajax({
            type: "POST",
            url: baseURL + "StudentInfo/Delete",
            contentType: "application/json",
            data: JSON.stringify(id),
            success: function(r){
                if(r.code == 0){
                    alert('操作成功', function(){
                        vm.reload();
                    });
                }else{
                    alert(r.msg);
                }
            }
        });
    });
}
var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        StudentInfo: {},
        studentSchoolId: null
    },
    methods: {
        query: function () {
            $("#exportExcel").attr('href',baseURL+'StudentInfo/export?token='+token+"&searching="+vm.q.key);

            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.StudentInfo = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "StudentInfo/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.StudentInfo = r.StudentInfo;
                vm.studentSchoolId = r.StudentInfo.studentSchoolId;
            });
        },
        changeTime: function(){
            this.StudentInfo.studentEnrollDate = $("#enrollDate").val();
            this.StudentInfo.studentBirthday = $("#birthday").val();
            this.StudentInfo.studentCurrentGrade = $("#currentGrade").val();
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "StudentInfo/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        pullin: function () {
            confirm('确定要开始数据导入？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "Pullin/getStudentInfoData",
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            $("#aaa").validate(
                {
                    debug:false, //调试模式，即使验证成功也不会跳转到目标页面

                    rules:{//配置验证规则，key就是被验证的dom对象，value就是调用验证的方法(也是json格式)
                        id:{
                            required:true,
                            number:true,
                            rangelength:[12,12]
                        },
                        className:{
                            required:true
                        },
                        name:{
                            required:true
                        },
                        sex:{
                            required:true
                        },
                        birthday :{
                            required:true //必填。如果验证方法不需要参数，则配置为true
                        },
                        ethnic:{
                            required:true,
                            isChinese:true
                        },
                        collage:{
                            required:true,
                            isChinese:true
                        },
                        professionName:{
                            required:true
                        },
                        schoolingYear:{
                            required:true,
                            range:[0,8]
                        },
                        currentGrade:{
                            required:true
                        },
                        enrollDate:{
                            required:true
                        },
                        telNumber:{
                            required:true,
                            isMobile:true
                        },
                        professionCode:{
                            required:true,
                            number:true,
                            rangelength:[4,4]
                        }
                    },
                    messages:{
                        id:{
                            required:"必填字段",
                            number:"请输入数字",
                            rangelength:"请输入12位长数字"
                        },
                        className:{
                            required:"必填字段"
                        },
                        name:{
                            required:"必填字段"
                        },
                        sex:{
                            required:"必填字段"
                        },
                        birthday :{
                            required:"必填字段"
                        },
                        ethnic:{
                            required:"必填字段",
                        },
                        collage:{
                            required:"必填字段"
                        },
                        professionName:{
                            required:"必填字段"
                        },
                        schoolingYear:{
                            required:"必填字段",
                            range:"请输入一个介于0-8之间的数字"
                        },
                        currentGrade:{
                            required:"必填字段"
                        },
                        enrollDate:{
                            required:"必填字段"
                        },
                        telNumber:{
                            required:"必填字段",
                            phone:"请输入正确的电话号码"
                        },
                        professionCode:{
                            required:"必填字段",
                            rangelength:"请输入4位数字"
                        }
                    },
                    submitHandler:function() {
                        var url = vm.StudentInfo.studentSchoolId == vm.studentSchoolId ? "StudentInfo/Update" : "StudentInfo/Insert";
                        $.ajax({
                            type: "POST",
                            url: baseURL + url,
                            contentType: "application/json",
                            data: JSON.stringify(vm.StudentInfo),
                            success: function(r){
                                if(r.code === 0){
                                    alert('操作成功', function(){
                                        vm.reload();
                                    });
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'studentSchoolId': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
        // exportExcel:function () {
        //     $.get(
        //         baseURL + "StudentInfo/export/" + vm.studentSchoolId, function (r) {
        //             console.log("读取jsonName OK!");
        //         }
        //     )
        // }
    }
});